package id.pratama.ongkirjne;

import id.pratama.ongkirjne.fragment.Fr_Hasil;
import id.pratama.ongkirjne.fragment.Fr_Input;
import id.pratama.ongkirjne.fragment.Fr_Input.Fr_inputListener;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;

import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends SherlockFragmentActivity implements Fr_inputListener
{
	private static final String	TAG	= MainActivity.class.getName();

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF9105")));

		// cek dia single pane atau dual pane
		if (findViewById(R.id.fragment_container) != null)
		{
			if (savedInstanceState != null)
			{
				return;
			}

			Fr_Input fragment_input = new Fr_Input();
			fragment_input.setArguments(getIntent().getExtras());
			getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fragment_input).commit();
		}
	}

	@Override
	public void onFragmentInputButtonClick(Bundle b)
	{
		Fr_Hasil fragment_hasil = (Fr_Hasil) getSupportFragmentManager().findFragmentById(R.id.fr_hasil);

		if (fragment_hasil != null)
		{
			Utils.TRACE(TAG, "fragment ada");
			fragment_hasil.updateHasil(b);
		} else
		{
			Utils.TRACE(TAG, "fragment tidak ada");
			Utils.TRACE(TAG, b.getString(Fr_Input.TAG_ASAL));
			Fr_Hasil fr_hasil = new Fr_Hasil();
			fr_hasil.setArguments(b);
			FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
			trans.replace(R.id.fragment_container, fr_hasil);
			trans.addToBackStack(null);
			trans.commit();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		SubMenu sub = menu.addSubMenu("About");
		sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Toast.makeText(this, "Created By", Toast.LENGTH_SHORT).show();
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setMessage("Created By\n Pratama Wijaya");

		dialog.show();

		return super.onOptionsItemSelected(item);
	}

}
